<template>
	<view class="page">
		<uni-title :title="machineInfo.name" type="h2" align="center"></uni-title>
		<uni-section title="经营信息" type="line">
			<uni-list :border="false">
				<uni-list-item title="今日实收" :rightText="machineInfo.todayAmount || 0"/>
				<uni-list-item title="本月实收" :rightText="machineInfo.thisMonthAmount || 0"/>
			</uni-list>
			<view class="flex-row-around">
				<view class="flex-column-around action-box" @click="handleRestart">
					<uni-icons type="refreshempty" color="#5e5e5e" size="26"></uni-icons>
					<text>系统重启</text>
				</view>
				<view class="flex-column-around action-box" @click="handleStopSale">
					<uni-icons type="locked" :color="machineInfo.izEnable == 1 ? '#ff5500' : '#55ff00'" size="26"></uni-icons>
					<text :style="{color: machineInfo.izEnable == 1 ? '#ff5500' : '#55ff00'}">{{machineInfo.izEnable == 1 ? '暂停销售' : '开启销售'}}</text>
				</view>
			</view>
		</uni-section>
		<uni-section title="运行信息" type="line">
			<uni-list :border="false">
				<uni-list-item title="设备状态" :rightText="machineInfo.faultState == 1 ? '正常' : '故障'"/>
				<uni-list-item title="电源状态" :rightText="machineInfo.ps == 1 ? '正常' : '断电'"/>
				<uni-list-item title="网络状态" :rightText="machineInfo.izOnline == 1 ? '在线' : '离线'"/>
				<uni-list-item title="销售状态" :rightText="machineInfo.izEnable == 1 ? '开启' : '暂停'"/>
				<uni-list-item title="总库存" :rightText="machineInfo.saveStock || 0"/>
				<uni-list-item title="缺货率" :rightText="machineInfo.stockRate || 0"/>
			</uni-list>
		</uni-section>
		<uni-section title="设备信息" type="line">
			<uni-list :border="false">
				<uni-list-item title="设备编号" :rightText="machineInfo.mid"/>
				<uni-list-item title="设备名称" :rightText="machineInfo.name"/>
				<uni-list-item title="机器号" :rightText="machineInfo.id"/>
				<uni-list-item title="详细地址" :rightText="machineInfo.address"/>
			</uni-list>
		</uni-section>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				machineId: "",
				machineInfo: {}
			}
		},
		
		onLoad(options) {
			this.machineId = options.machineId
			
			this.getMachineInfo()
		},
		
		methods: {
			getMachineInfo() {
				uni.showLoading({
					title: '加载中...'
				})
				uni.request({
					url: getApp().globalData.apiUrl + '/card/api/v1/getMachineDetail',
					data: {
						machineId: this.machineId
					},
					success: res => {
						if(res.data.success) {
							this.machineInfo = res.data.result
							uni.hideLoading()
						} else {
							uni.showToast({
								icon:'error',
								title: res.data.message
							})
						}
					}
				})
			},

			
			handleStopSale() {
				uni.request({
					url: getApp().globalData.apiUrl + '/card/api/v1/switchMachine',
					method:"POST",
					data: {
						machineId: this.machineId,
						onSale: this.machineInfo.izEnable == 1 ? '0' : '1'
					},
					success: res => {
						if(res.data.success) {
							uni.showToast({
								icon: 'success',
								title: '操作成功',
								duration: 2000,
								mask: true
							})
							setTimeout(() => {
								this.getMachineInfo()
							}, 2000)
						} else {
							uni.showToast({
								icon:'error',
								title: res.data.message
							})
						}
					}
				})
			},
			
			
			handleRestart() {
				uni.request({
					url: getApp().globalData.apiUrl + '/card/api/v1/reboot',
					method:"POST",
					data: {
						machineId: this.machineId
					},
					success: res => {
						if(res.data.success) {
							uni.showToast({
								icon: 'success',
								title: '操作成功',
								duration: 2000,
								mask: true
							})
							setTimeout(() => {
								this.getMachineInfo()
							}, 2000)
						} else {
							uni.showToast({
								icon:'error',
								title: res.data.message
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background-color: #fff;
	}
	.action-box {
		height: 140rpx;
		padding: 10rpx;
		
		text {
			color: #5e5e5e;
		}
	}
</style>